<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input
        v-model="params.name"
        clearable
        placeholder="请输入内容"
        class="search-main"
      />
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>
    <!-- 添加楼宇 -->
    <!-- <div class="create-container"> -->
    <el-button type="primary" @click="addBuilding">添加楼宇</el-button>
    <!-- </div> -->
    <!-- 添加楼宇弹框 -->
    <el-dialog
      title="添加楼宇"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <!-- <span>这是一段信息</span> -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="list">
        <el-table-column type="index" label="序号" />
        <el-table-column label="楼宇名称" width="180" prop="name" />
        <el-table-column label="层数" prop="floors" />
        <el-table-column label="在管面积(m²)" width="120" prop="area" />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column label="状态" prop="status">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button size="mini" type="text" @click="bianji(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="confirmDel(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-change="pageChange"
      />
      <!-- size-change(时间名称) pageSize改变时触发  回调参数(每页页数)
      current-change(时间名称) currentPage
      -->
    </div>
  </div>
</template>

<script>
import { getExterpriseBuildingaAPI, createBuildingAPI, delBuildingListAPI, editBuildingListAPI } from '@/api/enterprise'
export default {
  name: 'Building',
  data() {
    return {
      params: {
        page: 1,
        pageSize: 10,
        name: ''
      },
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addFormRules: {
        name: [{ required: true, message: '请输入楼宇名称', trigger: 'blur' }],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [{ required: true, message: '请输入在管面积', trigger: 'blur' }],
        propertyFeePrice: [
          { required: true, message: '请输入物业费', trigger: 'blur' }
        ]
      },
      list: [],
      total: 0,
      dialogVisible: false
    }
  },
  created() {
    this.getList()
    if (this.id) {
      this.getPist()
    }
  },
  methods: {
    async getList() {
      const res = await getExterpriseBuildingaAPI(this.params)
      // console.log(res.data)
      this.list = res.data.rows
      // 总条数
      this.total = res.data.total
    },
    doSearch() {
      // 1. 重置page为1
      this.params.page = 1
      // 2. 调用获取列表接口
      this.getList()
    },
    // 分页切换
    pageChange(page) {
      // 参数更改
      this.params.page = page
      // 渲染页面
      this.getList()
    },
    // 业务背景：status为0表示闲置中，status为1表示租赁中
    // 解决思路：通过映射的方式找到状态码和中文的对应关系，然后根据状态码返回对应的中文
    formatStatus(status) {
      const statusMap = {
        0: '租赁中',
        1: '闲置中'
      }
      return statusMap[status]
    },
    addBuilding() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
    },
    // 新增
    confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          if (this.addForm.id) {
            // this.title = '修改楼宇'
            await editBuildingListAPI(this.addForm)
          } else {
            console.log('添加')
            // 1. 提交接口
            await createBuildingAPI(this.addForm)
          }
          // 2. 获取列表
          this.getList()
          // 3. 关闭弹框
          this.dialogVisible = false
        }
      })
    },
    // 删除
    async confirmDel(id) {
      this.$confirm('你确定要删除吗?', '温馨提示', {
        type: 'warning'
      })
        .then(async() => {
          console.log(id)
          await delBuildingListAPI(id)
          this.$message.success('删除成功')
          this.getList()
        })
        .catch(() => {})
    },
    // 编辑
    async bianji(row) {
      // 1. 打开弹框
      this.dialogVisible = true
      // 2. 解构必要字段
      const { id, area, floors, name, propertyFeePrice } = row
      this.addForm = {
        id, area, floors, name, propertyFeePrice
      }
      // const res = await getDetailAPI(id)
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-label {
    width: 100px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.page-container {
  padding: 4px 0px;
  text-align: right;
}
.create-container {
  margin: 10px 0px;
}
</style>
